//
//  DDMainViewController.swift
//  swift微博
//
//  Created by whitcast on 16/7/9.
//  Copyright © 2016年 whitcast. All rights reserved.
//

import UIKit
import SVProgressHUD

class DDMainViewController: UITabBarController {
    
    override func loadView() {
        super.loadView()
        //使用KVC 赋值只读属性
        //修改tabbar
        setValue(DDTabbarView(), forKey: "tabBar")
        
        let customTabbar = valueForKey("tabBar") as? DDTabbarView
        
        customTabbar?.writeBtnClourse = {
            //截屏
            let img = self.clipScreen()
            //跳转控制器
            let vc = DDWriteBtnVC()
            
            vc.backImg = img
            
            SVProgressHUD.dismiss()
            
            self.presentViewController(vc, animated: false, completion: nil)
            
             vc.show()
            
        }
        
    }
    
    //截屏
    private func clipScreen()->UIImage{
        
        let window = UIApplication.sharedApplication().keyWindow!
        //截屏
        UIGraphicsBeginImageContext(window.bounds.size)
        
        window.drawViewHierarchyInRect(window.bounds, afterScreenUpdates: false)
        
        let img = UIGraphicsGetImageFromCurrentImageContext()
        //关闭上下文
        UIGraphicsEndImageContext()
        
        return img
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //添加子控制器
        addChildViewController(DDHomdeTableViewController(), title: "主页",imageName: "tabbar_home")

        addChildViewController(DDMessageTableViewController(), title: "消息",imageName:"tabbar_message_center")
        
        addChildViewController(DDDiscoveryTableViewController(), title: "探索",imageName: "tabbar_discover")
        
        addChildViewController(DDMessageTableViewController(), title: "我的",imageName: "tabbar_profile")
        
    }
    
    //添加自控制器
    func addChildViewController(childVC : UIViewController,title :String,imageName :String)->() {
        //子控制器是 UINavigationController
        let nav = UINavigationController.init(rootViewController: childVC)
        //导航栏标题
        childVC.navigationItem.title = title
        
        //tabbar标题
        childVC.tabBarItem.title = title
        
        //设置字体选中的时候的颜色
        childVC.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        
        //一般情况下 和 选中时的图片  改变渲染模式
        childVC.tabBarItem.image = UIImage(named:imageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        childVC.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //添加到tabbar控制器
        addChildViewController(nav)
    }
}

//"tabbar_home_selected"